﻿using Loki.Utils;

namespace Loki.Model
{
    public abstract class ApplicationService : LokiService
    {
        protected EntityContainer<TEntity, TKey> CreateContainer<TEntity, TKey>(ContainerConfiguration<TEntity, TKey> configuration)
            where TEntity : IKeyedEntity<TKey>
        {
            EntityContainer<TEntity, TKey> container = new EntityContainer<TEntity, TKey>(configuration);
            try
            {
                RegisterDisposable(container);
                return container;
            }
            catch
            {
                if (container != null)
                {
                    container.Dispose();
                }

                throw;
            }
        }

        protected EntityLinkContainer<TKey> CreateLinkContainer<TKey>(LinkContainerConfiguration<TKey> configuration)
        {
            EntityLinkContainer<TKey> container = new EntityLinkContainer<TKey>(configuration);
            try
            {
                RegisterDisposable(container);
                return container;
            }
            catch
            {
                if (container != null)
                {
                    container.Dispose();
                }

                throw;
            }
        }
    }
}